package com.tdy.nht.web.operate.controller.doc;

import cn.hutool.core.collection.CollUtil;
import com.tdy.nht.common.Result;
import com.tdy.nht.common.ResultPage;
import com.tdy.nht.constant.StatusCodeConstants;
import com.tdy.nht.doc.api.${modelName}Api;
import com.tdy.nht.doc.domain.${modelName}DTO;
import com.tdy.nht.doc.domain.${modelName}Query;
#if($!saveMethod == '1')
import com.tdy.nht.web.operate.annotation.Token;
#end
import com.tdy.nht.web.operate.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
* @Description: $!{annotation}Controller
* @author: $!{author}
* @Date: ${createTime}
*/
@Controller
@RequestMapping("/nht/doc/${lowerModelName}")
public class ${modelName}Controller extends BaseController {


    @Autowired
    private ${modelName}Api ${lowerModelName}Api;

    /**
    * @Description: 列表页面
    * @author: yinqi
    * @date: 2018/9/4 21:45
    * @return: ModelAndView
    */
    @GetMapping("/queryPage")
        public ModelAndView queryPage() {
        ModelAndView andView = new ModelAndView();
        andView.setViewName("doc/$!{pageName}_list");
        return andView;
    }

    /**
    * 异步加载列表数据
    *
    * @param query 查询条件
    * @return 响应结果
    */
    @ResponseBody
    @PostMapping("/loadData")
    public ResultPage<${modelName}DTO> loadData(${modelName}Query query) {
       ResultPage<${modelName}DTO> results = ${lowerModelName}Api.list${modelName}Page(query);
       return results;
    }

        /**
        * @Description: 显示对话框
        * @param: id 查询详情数据的Ids
        * @return: ModelAndView
        */
        @GetMapping("/showDetail")
        public ModelAndView showDetail(@RequestParam(name = "id") Long id) {
        ModelAndView modelAndView = new ModelAndView();
        Result<${modelName}DTO> result = ${lowerModelName}Api.get${modelName}ById(id);
            modelAndView.addObject("${lowerModelName}", result.getData());
            modelAndView.setViewName("doc/$!{pageName}_detail");
            return modelAndView;
            }

            /**
            *  批量删除记录
            * @param ids 记录id
            * @return 返回响应结果
            */
            @PostMapping("/remove")
            @ResponseBody
            public Result<Object> remove(@RequestParam("id") List<Long> ids) {
                Result<Object> result = new Result<>();
                if (CollUtil.isEmpty(ids)) {
                   result.setCode(StatusCodeConstants.PARAMS_ERROR);
                   return result;
                }
                result = ${lowerModelName}Api.remove${modelName}Batch(ids);
                return result;
             }

    #if($!saveMethod == '1')

        /**
        * 跳转到新建$!{annotation}页面 如果id存在则是编辑
        * @param id 记录id
        * @return 响应结果
        */
        @Token(save = true)
        @GetMapping("/showAddPage")
        public ModelAndView showAddPage(@RequestParam(name = "id", required = false) Long id) {
            ModelAndView andView = new ModelAndView();
            Result<${modelName}DTO> result = new Result<>();
            if (id != null) {
                result = ${lowerModelName}Api.get${modelName}ById(id);
            }
            andView.addObject("${lowerModelName}", result.getData());
            andView.setViewName("doc/$!{pageName}_add");
            return andView;
        }

        /**
        * $!{annotation}保存方法
        * @param ${lowerModelName}DTO 保存实体
        * @return 响应结果
        */
        @Token(remove = true)
        @ResponseBody
        @PostMapping("/save${modelName}")
            public Result<Object> save${modelName}(${modelName}DTO ${lowerModelName}DTO) {
            Result<Object> result = ${lowerModelName}Api.save${modelName}(${lowerModelName}DTO);
            return result;
        }
    #end
}
